﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SpyApp.Models
{
    public class SortingPagingInfo
    {
        public string SortField { get; set; }
        public string SortDirection { get; set; }
        public int PageSize { get; set; }
        public int PageCount { get; set; }
        public int CurrentPageIndex { get; set; }
    }

    public class NghiaPageList
    {
        public NghiaPageList(int totalItemCount, int pageNumber, int pageSize)
        {
            if (pageNumber < 1)
                throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
            if (pageSize < 1)
                throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
            if (totalItemCount < 0)
                throw new ArgumentOutOfRangeException("totalItemCount", totalItemCount, "totalItemCount cannot be less than 0.");

            // set source to blank list if superset is null to prevent exceptions
            this.TotalItemCount = totalItemCount;
            this.PageIndex = pageNumber;
            this.PageSize = pageSize;

            this.PageCount = TotalItemCount > 0
                            ? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
                            : 0;
            HasPreviousPage = PageIndex > 1;
            HasNextPage = PageIndex < PageCount;
            IsFirstPage = PageIndex == 1;
            IsLastPage = PageIndex >= PageCount;
            FirstItemOnPage = (PageIndex - 1) * PageSize + 1;
            var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1;
            LastItemOnPage = numberOfLastItemOnPage > TotalItemCount
                            ? TotalItemCount
                            : numberOfLastItemOnPage;

            // add items to internal list
            if (TotalItemCount > 0)
                if (pageNumber == 1) 
                    Skip = 0;
                else 
                    Skip = pageNumber - 1 * PageSize;
        }

        public bool HasPreviousPage { get; set; }
        public bool HasNextPage { get; set; }
        public bool IsFirstPage { get; set; }
        public bool IsLastPage { get; set; }
        public int TotalItemCount { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int PageCount { get; set; }
        public int FirstItemOnPage { get; set; }
        public int LastItemOnPage { get; set; }
        public int Skip { get; set; }
    }
}